using System;
using System.Collections.Generic;
using Cinemachine;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty
{

    [System.Serializable]
    public class BehaviorCamera
    {
        public string cameraName;
        public CinemachineVirtualCamera virtualCamera;
    }
    
    public class BehaviorCameraCollection : MonoBehaviour
    {
        public List<BehaviorCamera> cameras;
        private Dictionary<string, BehaviorCamera> _cameraMap;

        public SyncTransform syncTransform;
        
        private void Awake()
        {
            syncTransform = GetComponent<SyncTransform>();
            InitMap();
        }


        private void InitMap()
        {
            _cameraMap = new Dictionary<string, BehaviorCamera>();
            if(cameras == null) return;
            
            foreach (var behaviorCamera in cameras)
            {
                if (behaviorCamera!=null)
                {
                    if (behaviorCamera.virtualCamera)
                    {
                        behaviorCamera.virtualCamera.m_Priority = 0;
                    }
                    _cameraMap[behaviorCamera.cameraName] = behaviorCamera;
                }
            }
        }

        public BehaviorCamera GetBehaviorCamera(string cameraName)
        {
            if (!Application.isPlaying)
            {
                InitMap();
            }
            _cameraMap.TryGetValue(cameraName, out var bCamera);
            return bCamera;
        }

        public void SwitchSync(bool enable)
        {
            if (syncTransform)
            {
                syncTransform.enabled = enable;
            }
        }
    }
}